package com.ali.gts.servlet.controller;

import com.ali.gts.servlet.domain.Book;
import com.ali.gts.servlet.domain.Page;
import com.ali.gts.servlet.service.BookService;
import com.ali.gts.servlet.service.impl.BookServiceImpl;
import com.ali.gts.servlet.util.WebUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class BookServlet extends BaseServlet{

    private BookService bookService = new BookServiceImpl();


    /**
     * 处理分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 pageNo和pageSize
        int pageNo = WebUtil.parseInt(req.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用BookService.page（pageNo,pageSize）;Page对象
        Page<Book> page = bookService.page(pageNo,pageSize);
        //设置后台请求地址
        page.setUrl("/client/bookServlet?action=page");
        //3.保存Page对象到Request域中
        req.setAttribute("page",page);
        //4.请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/client/manager/book_manager.jsp").forward(req,resp);
    }


    protected void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int pageNo = WebUtil.parseInt(req.getParameter("pageNo"),0);
        pageNo+=1;
        //1.获取请求的参数==封装成为Book对象
        Book book = WebUtil.copyParamToBean(req.getParameterMap(), new Book());

        //2.调用BookService.addBook()保存图书
        bookService.addBook(book);

        //3、转发到图书列表页面 不能用转发，转发是同一个请求，表单提交完成后，浏览器
        // 会记录下最后一次请求的全部信息，此时按功能键 F5 此时就会发起浏览器记录的最后一次请求
        //这里添加图书成功后，每次重定向时，传的参数就是查最后一页的数据
        resp.sendRedirect(req.getContextPath() + "/client/bookServlet?action=page&pageNo="+ req.getParameter("pageNo"));

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtil.parseInt(req.getParameter("pageNo"),0);
        pageNo+=1;
        //1.获取请求的参数id,图书编号
        int id = WebUtil.parseInt(req.getParameter("id"),0);
        //2.调用bookService.deleteBookById();删除图书
        bookService.deleteBookById(id);
        //3.重定向回图书列表的管理页面
        resp.sendRedirect(req.getContextPath()+"/client/bookServlet?action=page&pageNo="+ req.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.获取请求的参数==封装成为Book对象
        Book book = WebUtil.copyParamToBean(req.getParameterMap(),new Book());
        //2.调用BookService.updateBook(book);修改图书
        bookService.updateBook(book);
        //3.重定向回图书列表管理页面
        //resp.sendRedirect(req.getContextPath()+"/client/bookServlet?action=list");
        resp.sendRedirect(req.getContextPath() + "/client/bookServlet?action=page");
    }

    /**
     * 获取要修改的图书信息，然后跳转到图书修改的页面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 图书编号
        int id = WebUtil.parseInt(req.getParameter("id"),0);
        //2.调用bookService.queryBookById查询图书
        Book book = bookService.queryBookById(id);
        //3.保存图书到Request域中
        req.setAttribute("book",book);
        //4.请求转发到 pages/manager/book_edit.jsp页面
        req.getRequestDispatcher("/pages/book/book_edit.jsp").forward(req,resp);
    }

    //使用了分页技术后 该方法就不用了
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 通过 BookService 查询全部图书
        List<Book> books = bookService.queryBooks();
        //2 把全部图书保存到 Request 域中
        req.setAttribute("books", books);
        //3、请求转发到/pages/manager/book_manager.jsp 页面
        req.getRequestDispatcher("/client/manager/book_manager.jsp").forward(req,resp);
    }



    /**
     * 处理价格区间和分页
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 pageNo和pageSize
        int pageNo = WebUtil.parseInt(req.getParameter("pageNo"),1);
        int pageSize = WebUtil.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtil.parseInt(req.getParameter("min"), 0);
        int max = WebUtil.parseInt(req.getParameter("max"), Integer.MAX_VALUE);

        //2.调用BookService.page（pageNo,pageSize）;Page对象
        Page<Book> page = bookService.pageByPrice(pageNo,pageSize,min,max);

        StringBuilder sb = new StringBuilder("/client/bookServlet?action=pageByPrice");
        //如果参数里面有最小的值url上追加 min
        if (req.getParameter("min") != null){
            sb.append("&min=").append(min);
        }
        if (req.getParameter("max") != null){
            sb.append("&max=").append(max);
        }
        //设置后台请求地址这里 要写成pageByPrice
        page.setUrl(sb.toString());
        //3.保存Page对象到Request域中
        req.setAttribute("page",page);
        //4.请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/client/manager/book_manager.jsp").forward(req,resp);
    }




}
